<?php
/**
 * Created by PhpStorm.
 * User: 李洋波
 * 收货扫描类
 * Date: 2016/12/12
 * Time: 11:12
 */

class ReceivingScan extends MY_Controller{
    function __construct() {
        parent::__construct();
        //加载维修订单模型
        $this->load->model('scan_model');
    }

    /**
     * 产品扫描
     */
    public function productScanList(){
        $gp=gp();
        if($gp['order_num'] || $gp['pid']) {
            $data = $this->scan_model->getProductList(trim($gp['order_num']),$gp['pid']);
            $language=$this->uri->segment(1);
            foreach($data as &$row){
                $status=$this->common_model->selectFirst('mt_product_status',array('id'=>$row['status_id']));
                switch($language){
                    case 'sc':
                        $row['status_name']=$status['name_ch'];
                        break;
                    case 'english':
                        $row['status_name']=$status['name_en'];
                        break;
                    case 'es':
                        $row['status_name']=$status['name_es'];
                        break;
                    default:
                        $row['status_name']=$row['name_en'];
                        break;
                }
            }
            unset($row);
            $data['productList'] = $data;
            $data['order_num']=$gp['order_num']?$gp['order_num']:$data[0]['order_num'];
        }else{
            $data['productList'] = array();
            $data['order_num']='';
        }
        //视图以及样式
        $this->load->view('defaults/order_scan',$data);
        $this->load->view('defaults/common/footer');
    }

    /**
     * 产品收货
     */
    public function productReceiving(){
        $gp=gp();
        $data = $this->scan_model->getProductList($gp['order_num']);
        if($gp['ids']) {
            foreach($gp['ids'] as $key=>$val) {
                $products = $this->common_model->selectFirst('mt_product',array('id'=>$val));
                if($products && $products['status_id']==1){
                    $this->common_model->resetStatus($val);
                }
            }
            $arrivalNum= $this->common_model->countRows('mt_product',array('order_num'=>$gp['order_num'],'status_id >'=>1));
            $arrivalRate=round($arrivalNum/count($data),2);
            $this->scan_model->updateOrderInfo($gp['order_num'], array('arrivalRate' => ($arrivalRate * 100) . '%'));
        }
        redirect(blang_url('orders/ReceivingScan/productScanList/?order_num='.$gp['order_num']));
    }
}